/**
 * JsHttpRequest
 *
 * Copyright (C) 2007 Firma JAKUBIAK Antoni Jakubiak
 * http://www.jakubiak.biz/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/**
 * http://en.wikipedia.org/wiki/XMLHttpRequest
 */
JSHttpResponse = null;
function JSHttpRequest() {
	this.onreadystatechange = null;
	this.readyState         = 0;
	this.responseText       = null;
	this.responseXML        = null;
	this.status             = null;
	this.statusText         = null;
	this.headNode           = document.getElementsByTagName("head").item(0);
}
/**
 * Cancels the current request
 */
JSHttpRequest.prototype.abort = function() {
	this.headNode.removeChild( this.jsNode );
	this.jsNode       = null;
	this.status       = null;
	this.statusText   = null;
	this.responseText = null;
	this.responseXML  = null;
}
/**
 * Returns the complete set of HTTP headers as a string
 */
JSHttpRequest.prototype.getAllResponseHeaders = function() {
	return null;
}
/**
 * Returns the value of the specified HTTP header
 * @param iHeaderName - kod naglowka
 */
JSHttpRequest.prototype.getResponseHeader = function( iHeaderName ) {
	return null;
}
/**
 * Specifies the URL. Optional attributes are ignored
 * @param iMethod ignored (only GET is supported)
 * @param iURL - url to GET
 * @param iAsync ignored - (only true is supported)
 * @param iUserName ignored
 * @param iPassword ignored
 */
JSHttpRequest.prototype.open = function( iMethod, iURL, iAsync, iUserName, iPassword ) {
	this.method   = "GET";
	this.URL      = iURL;
	this.async    = true;
	this.userName = null;
	this.password = null;
}
/**
 * Sends the request.
 * @param iContent ignored
 */
JSHttpRequest.prototype.send = function( iContent ) {
	this.content           = null;
	this.jsNode            = document.createElement("script");
	this.jsNode.type       = "text/javascript";
	this.jsNode.charset    = "utf-8";
	this.jsNode.src        = this.URL;
	var oThis              = this;
	var onLoad             = function() {
		if ( JSHttpResponse ) {
			var response       = JSHttpResponse.get();
			oThis.responseXML  = response.responseXML;
			oThis.responseText = response.responseText;
			JSHttpResponse     = null;
		} else {
			oThis.responseXML  = null;
			oThis.responseText = null;
		}
		oThis.status     = 200;
		oThis.statusText = "OK";
		oThis.readyState = 4;
		if ( oThis.onreadystatechange ) {
			oThis.onreadystatechange();
		}
	}
	var onReadyStateChange = function( iEvent ) {
		var e = (iEvent?iEvent:window.event).target?(iEvent?iEvent:window.event).target:(iEvent?iEvent:window.event).srcElement;
		if( e.readyState=="loaded" || e.readyState=="complete") {
			onLoad();
		}
	}
	if(navigator.product=="Gecko"){
		this.jsNode.onload = onLoad;
	}else{
		this.jsNode.onreadystatechange = onReadyStateChange;
	}
	this.headNode.appendChild(this.jsNode);
	this.readyState = 1;
	if ( this.onreadystatechange ) {
		this.onreadystatechange();
	}

}
/**
 * Ignored - Adds a label/value pair to the HTTP header to be sent.
 */
JSHttpRequest.prototype.setRequestHeader = function( iLabel, iValue ) {
	return null;
}
